package com.example.photoview;

//import com.example.PPT.DisplayWindow;
import com.example.showImage.ImageWindow;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.input.Clipboard;
import javafx.scene.input.DataFormat;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.w3c.dom.Text;

import java.io.File;
import java.io.IOException;
import java.util.List;
/**
 * 完成图片左右键完成的操作
 */
import java.util.List;

public class ImageBox extends VBox {
    //4.16
    //private ImageModel im;
    private HomeController homeController=(HomeController) ControllerUtil.controllers.get(HomeController.class.getSimpleName());


    private ImageModel imageModel;
    private ImageView2 imageView2;
    private ContextMenu contextMenu;//右键框
    private  ContextMenu nullMenu;
    private CheckBox checkBox = new CheckBox();

    /**
     * 构造器，初始化一个缩略图单元
     *
     * @param imageModel 图片文件类
     */
    public ImageBox(ImageModel imageModel) {
        this.imageModel = imageModel;
        ImageView2 imageView = new ImageView2(new Image(imageModel.getImageFile().toURI().toString(),
                100,
                100,
                true,
                true,
                true));
        this.imageView2 = imageView;                                //图片
        Label imageLabel = new Label(imageModel.getImageName());  //标签 - 文件名
        imageLabel.setStyle("-fx-padding:7 0 7 -2;");

        HBox hBox = new HBox(checkBox, imageLabel);
        hBox.setAlignment(Pos.CENTER);
        hBox.setStyle("-fx-padding:5 5 3 5;");

         getChildren().addAll(imageView, hBox);//将图片加入面板
        setMaxSize(170, 170);
        setAlignment(Pos.BOTTOM_CENTER);

        //设置文件信息tips
        String tooltip = String.format("名称: %s\n大小: %s", imageModel.getImageName(), imageModel.getFormLength());
        Tooltip.install(this, new Tooltip(tooltip));//鼠标移到的位置有提示

        DropShadow dropShadow=new DropShadow();
        dropShadow.setHeight(0);
        dropShadow.setWidth(0);
        initPopUpMenu(imageView);
       initMouseAction();
        initCheckBox();
      // nullMenu(homeController.getScrollPane());

        try {
            setOnKeyPressed(keyEvent -> {
                //鼠标点击事件
                System.out.println("keyPressed");
                setOnMouseClicked(event -> {
                    //左键
                    if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 1) {
                        ImageWindow dw = new ImageWindow();
                        dw.setImage(imageModel);
                        //System.out.println(imageModel.getImageName());
                        dw.start(new Stage());//产生看图片界面
                    }
                });
            });
        }catch (Exception e){
            System.out.println("just failed");
        }
    }

    /**
     * 设置选中与否的属性绑定
     */
    ImageBox imageBox = this;

    private void initCheckBox() {
        checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (newValue) {//新点击，就加入，否则移除
                    SelectionModel.add(imageBox);
                } else {
                    SelectionModel.remove(imageBox);
                }
                homeController.selectedNumLabel.setText("| 已选中"+SelectionModel.getImageModels().size()+" 张");
            }
        });

    }

    /**
     * 加载右键菜单
     */
    private void initPopUpMenu(ImageView2 imageView2) {

        contextMenu = new ContextMenu();
        MenuItem copy = new MenuItem("复制");
        MenuItem cut = new MenuItem("剪切");
        MenuItem paste = new MenuItem("粘贴");
        MenuItem rename = new MenuItem("重命名");
        MenuItem compress = new MenuItem("压缩");
        MenuItem delete = new MenuItem("删除");
        MenuItem atr = new MenuItem("属性");
        contextMenu.getItems().addAll(copy,cut,paste,rename,compress,delete,atr);
        copy.setOnAction(e->{
            new CopyAction(this);//复制操作
        });
        cut.setOnAction(e->{
            new CutAction(this);//剪切操作
        });
        rename.setOnAction(e->{
            new RenameAction(this);//重命名操作
        });
        paste.setOnAction(e->{
            try {
                new PasteAction(this);//粘贴操作
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        compress.setOnAction(e->{
            new CompressAction(this);//压缩操作
        });
        atr.setOnAction(e->{
            new AtrAction(this);//属性操作
        });
        delete.setOnAction(e->{
            new DeleteAction(this);//删除操作
        });
        imageView2.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent e) -> {
            if (e.getButton() == MouseButton.SECONDARY)//如果是右键
                contextMenu.show(imageView2, e.getScreenX(), e.getScreenY());//弹出菜单
            else {
                if (contextMenu.isShowing())
                    contextMenu.hide();
            }
        });
    }
/*private void nullPopUpMenu(Node node){
  contextMenu = new ContextMenu();
    MenuItem paste = new MenuItem("粘贴");
    MenuItem all = new MenuItem("全选");
    contextMenu.getItems().add(paste);
    contextMenu.getItems().add(all);
    paste.setOnAction(e->{
        try {
            new PasteAction(this);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    });
    all.setOnAction(e->{
        homeController.SelectAllAction();
    });
    node.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent e) -> {

        Node clickNode = e.getPickResult().getIntersectedNode();
        // System.out.println(clickNode.toString());

            if (e.getButton() == MouseButton.SECONDARY&& !(clickNode instanceof ImageBox) && !(clickNode instanceof Text)) {// 鼠标右键
                if (SelectedModel.getSourcePath() == null || SelectedModel.getCopyOrMove() == -1)  {
                    paste.setDisable(true);
                } else {
                    paste.setDisable(false);
                }
               contextMenu.show(node, e.getScreenX(), e.getScreenY());
            } else {
                System.out.println("single");
                if (contextMenu.isShowing()) {
                    contextMenu.hide();
                }
            }

    });

}*/
 public void nullMenu(Node node){
        nullMenu=new ContextMenu();
        MenuItem paste=new MenuItem("粘贴");
        MenuItem all=new MenuItem("全选");
        nullMenu.getItems().addAll(paste,all);
        paste.setOnAction(e->{
            try {
//                new PasteAction(this);
                SelectedModel.paste(homeController.getCurrentPath());

                nullMenu.hide();
                System.out.println("paste");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            nullMenu.hide();
        });
        all.setOnAction(e->{
         homeController.SelectAllAction();
        });
       node.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent e) -> {
           Node clickNode = e.getPickResult().getIntersectedNode();
            if (e.getButton() == MouseButton.SECONDARY&&!(clickNode instanceof ImageView2)){
                if (SelectedModel.getSourcePath() == null || SelectedModel.getCopyOrMove() == -1) {
                    paste.setDisable(true);
                }
                else
                    paste.setDisable(false);
                nullMenu.show(node, e.getScreenX(), e.getScreenY());
            }

            else {
                if (nullMenu.isShowing())
                    nullMenu.hide();
            }
        });
       if(nullMenu.isShowing()){
           System.out.println("135453435");
           nullMenu.hide();
       }
    }
    /**
     * 图片对鼠标操作的反馈
     */
    private void initMouseAction() {
        //鼠标点击事件
        setOnMouseClicked(event -> {
            //左键
            Node clickNode = event.getPickResult().getIntersectedNode();
            if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2) {
                // 鼠标左键双击看大图
                ImageWindow dw = new ImageWindow();
                dw.setImage(imageModel);
                //System.out.println(imageModel.getImageName());
                    dw.start(new Stage());//产生看图片界面
            }else  if (event.getButton() == MouseButton.PRIMARY&& !(clickNode instanceof ImageBox) && !(clickNode instanceof Text)) {
                SelectionModel.clear();
            }
        });



        //当鼠标指向时
        this.setOnMouseMoved(event -> {
            this.setStyle("-fx-background-color:rgba(0, 0, 0, 0.07);");
        });

        //当鼠标离开时
        this.setOnMouseExited(event -> {
            this.setStyle("-fx-background-color:transparent;");
        });

    }

    public ImageView2 getImageView2() {
        return imageView2;
    }

    public ImageModel getImageModel() {
        return imageModel;
    }

    public CheckBox getCheckBox() {
        return checkBox;
    }

    public ContextMenu getContextMenu() {
        return contextMenu;
    }

    public ContextMenu getNullMenu() {
        return nullMenu;
    }
}
